<?php
/**
 * 
 * @authors Liujinbi (857053791@QQ.com)
 * @date    2017-08-11 16:53:27
 * @version $Id$
 */
namespace app\employee\controller;

use think\Db;
use think\Request;

class Purchase extends Base {
    
    /**
     * 采购单列表
     * @return [type] [description]
     */
    function index(){
		$employee_id = $this->uid;

		$purchase = db()->table('sdzx_purchase')->alias('p')
		->join('sdzx_purchase_goods pg','p.purchase_id = pg.purchase_id')
		->field('COUNT(pg.pg_id) as num,SUM(pg.num*pg.price) as sum,p.create_time,p.purchase_id')
		->group('p.create_time')
		->where(['p.employee_id' => $employee_id,'p.status' => 0,'pg.status'=>0])
		->order('p.create_time desc')
		->select();
		foreach ($purchase as $key => $value) {
			$list = Db::name('purchase_goods')->where(['purchase_id'=>$value['purchase_id'],'status'=>0])->select();
			$finish = 1;
			/* 判断是否采购完 1--已采购完 0--未采购完 */
			foreach ($list as $k => $val) {
				if($val['num']!=$val['real_num']){
					$finish = 0;	
				}
			}
			$purchase[$key]['finish'] = $finish;
		}
		array_multisort(array_column($purchase,'finish'), SORT_ASC,array_column($purchase,'create_time'),SORT_ASC,$purchase);
		$this->assign('purchase',$purchase);
		return view();
    }
	
    /*采购详情*/
    public function purchaseList(){
    	$employee_id = $this->uid;
    	$purchase_id = request()->param('purchase_id');
    	$purchase = db()->table('sdzx_purchase')->alias('p')
    	->join('sdzx_purchase_goods pg','p.purchase_id = pg.purchase_id')
    	->join('sdzx_supplier s','pg.supplier_id=s.supplier_id','left')
    	->field('p.purchase_id,p.employee_name,p.create_time,pg.*,s.supplier_id,s.supplier_name,s.charge_phone')
    	->where(['p.employee_id' => $employee_id,'p.status' => 0,'pg.status'=>0,'p.purchase_id'=>$purchase_id])
    	// ->order('pg.real_num asc,pg.supplier_id asc,s.charge_phone asc')
    	->order('pg.real_num asc,pg.supplier_id asc')
    	->select();
    	foreach ($purchase as $key => $value) {
    		$finish = 0;
    		if($value['real_num'] == $value['num']){
    			$finish = 1;
    		}
    		$purchase[$key]['finish'] = $finish;
    	}
    	array_multisort(array_column($purchase,'finish'), SORT_ASC,array_column($purchase,'supplier_id'),SORT_ASC,$purchase);
    	$order = [];
    	foreach ($purchase as $k => $v) {
    		if ( !isset( $order[$v['purchase_id']]['purchase_id']  ) ) {
    			$order[$v['purchase_id']] = [
    				'purchase_id'      => $v['purchase_id'],
    				'employee_name'    => $v['employee_name'],
    				'create_time'      => $v['create_time'],
    				'status'           => $v['status'],
    				'create_name'      => $v['create_name'],
    				'total_price'      => 0,
    				'total_last_price' => 0
    			];
    		}

    		$_tmp = Db::field('o.order_id,o.receiver_name,o.receiver_mobile,og.num,sp.shop_name')
    		->table('sdzx_order_goods')->alias('og')
    		->join('sdzx_order o','og.order_id=o.order_id')
    		->join('sdzx_shop sp','o.user_id=sp.user_id','left')
    		->where("og.goods_specval_id={$v['specval_id']} and o.order_status=30")
    		->select();
    		$v['order'] = $_tmp;
    		$order[$v['purchase_id']]['list'][] = $v;
    	}
    	$supplier_list = Db::name('supplier')->select();
        // foreach ($supplier_list as $key => $value) {
        // 	$json_list[$key] = $value['supplier_name'];
        // }
		// foreach($purchase as $val){
			// $group_purchase[$val['purchase_id']]['list'][] = $val;
			// $group_purchase[$val['purchase_id']]['create_time'] = $val['create_time'];
		// }
    	$json_list = json_encode($supplier_list);
    	$this->assign(['purchase'=>$order,'supplier_list'=>$supplier_list,'json_list'=>$json_list]);
    	return view();
    }
	/* 实际采购 */
	public function update(){
		$pg_id = request()->param('pg_id');
		$real_num = request()->param('real_num');
		$cur_price = request()->param('cur_price');
		$specval_id = request()->param('specval_id');
		$supplier_id = request()->param('supplier_id');
		$pay_status = request()->param('pay_status');
		$apply_price = request()->param('apply_price');
		!$pg_id && $this->error('参数错误');
		!$specval_id && $this->error('参数错误');
		Db::startTrans();
		$result = Db::name('purchase_goods')->update(['pg_id' => $pg_id,'real_num' => $real_num,'cur_price' => $cur_price,'supplier_id'=>$supplier_id,'pay_status'=>$pay_status,'apply_price'=>$apply_price,'purchase_time'=>date('Y-m-d H:i:s')]);
		if($result !== false){
			$res = Db::name('goods_specval')->update(['specval_id' => $specval_id,'purchase_price' => $cur_price]);
			if($res !== false){
				Db::commit();
				$this->success('操作成功','refresh');
			}else{
				Db::rollback();
				$this->error('操作失败');
			}
		}else{
			Db::rollback();
			$this->error('操作失败');
		}
	}

	//取消采购
	public function cancel(){
		$pg_id = request()->param('pg_id');
		if(!$pg_id){
			$this->error('参数错误');
		}	
		if(request()->isPost()){
			$remark = request()->param('remarks');
			$data = [
				'entstock_num' 	=> 0,
				'real_num'   	=> 0,
				'status'		=> 2,
				'remark'		=> $remark,
				'cancel_time'	=> date('Y-m-d H:i:s')
			];
			$res = Db::name('purchase_goods')->where('pg_id',$pg_id)->update($data);
			if($res){
				$this->success('取消成功','refresh');
			}else{
				$this->error('取消失败');
			}
		}else{
			$this->assign('pg_id',$pg_id);
			return view();
		}
	}
}